# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=ocaml-lwt
_pkgname=lwt
pkgver=5.7.0
pkgrel=0
pkgdesc="OCaml promises and concurrent I/O"
url="https://github.com/ocsigen/lwt"
arch="all !riscv64 !loongarch64" # ocaml
license="MIT"
depends_dev="
	libev-dev
	ocaml-mmap-dev
	ocaml-ocplib-endian-dev
	ocaml-ppxlib-dev
	ocaml-react-dev
	ocaml-result-dev
	"
makedepends="$depends_dev dune dune-configurator ocaml ocaml-cppo"
options="!check"  # FIXME: tests hang
subpackages="$pkgname-dev ${pkgname}_react:_react ${pkgname}_ppx:_ppx"
source="$_pkgname-$pkgver.tar.gz::https://github.com/ocsigen/lwt/archive/$pkgver.tar.gz"
builddir="$srcdir/$_pkgname-$pkgver"
_ocamldir=usr/lib/ocaml

# 32-bit archs
case "$CARCH" in
	arm*|x86) options="$options textrels" ;;
esac

build() {
	dune exec \
		-p lwt,lwt_react,lwt_ppx \
		"src/unix/config/discover.exe" -- \
		--save --use-libev true

	dune build \
		-p lwt,lwt_react,lwt_ppx \
		--verbose
}

check() {
	dune runtest --verbose
}

package() {
	dune install \
		--destdir="$pkgdir" \
		--docdir=/.omit \
		lwt lwt_react lwt_ppx
	rm -Rf "$pkgdir"/.omit
}

dev() {
	default_dev
	depends="$depends_dev
		$pkgname=$pkgver-r$pkgrel
		${pkgname}_react=$pkgver-r$pkgrel
		${pkgname}_ppx=$pkgver-r$pkgrel"
	provides="${pkgname}_ppx-dev ${pkgname}_react-dev"

	cd "$pkgdir"

	local path; find usr/lib/ocaml \( \
			-name '*.cmt' -o \
			-name '*.cmti' -o \
			-name '*.ml' -o \
			-name '*.mli' \
		\) | while read -r path
	do
		amove "$path"
	done
}

_ppx() {
	pkgdesc="$pkgdesc (PPX syntax)"

	amove usr/lib/ocaml/lwt_ppx
}

_react() {
	pkgdesc="$pkgdesc (for FRP)"

	amove usr/lib/ocaml/lwt_react
}

prepare_py_provides() {
	local datadir="${subpkgdir:-$pkgdir}"
	local pkgbasedir=${pkgbasedir:-"$startdir/pkg"}
	local controldir="$pkgbasedir"/.control.${subpkgname:-$pkgname}

	options_has "!tracedeps" && return 0

	ocaml4-abuild-find provides \
		"$datadir"/usr/lib/ocaml \
		"$controldir" \
		"$pkgver-r$pkgrel"
}

scan_python3_dependency() {
	local controldir="$2" datadir="$3"

	ocaml4-abuild-find requires \
		"$datadir"/usr/lib/ocaml \
		"$controldir"
}

sha512sums="
42e629920783428673b99c9d7a639237c9e6b35079b5d907bc67e7ea506acf9edadc48cec580bdcfd2410ed9412bf5e6bcc8b09de2fa7d35ce1490973d05ddd1  lwt-5.7.0.tar.gz
"
